import { sysDictList } from '@/services/common';
import { monitorLimitsAdd, monitorLimitsUpdate } from '@/services/deviceMsg';
import {
  ModalForm,
  ProFormDigit,
  ProFormSelect,
  ProFormSwitch,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, Col, Form, Row, message } from 'antd';
import React, { useImperativeHandle, useState } from 'react';

const ItemOption = React.forwardRef((props: any, ref) => {
  const { refreshList } = props;
  const [form] = Form.useForm<any>();
  const [modalOpen, setModalOpen] = useState(false);
  const [modItem, setModItem] = useState<any>(null);
  const handleModalShow = async (row?: any) => {
    setModItem(row || null);
    if (!modalOpen) {
      form.resetFields();
    }
    if (row) {
      form.setFieldsValue({
        ...row,
        alarmSwitch: row.alarmSwitch === '1' ? true : false,
        ageArea: row.ageArea && String(row.ageArea),
        sex: row.sex && String(row.sex),
      });
    }
    setModalOpen(!modalOpen);
  };
  useImperativeHandle(ref, () => ({
    handleModalShow,
  }));
  return (
    <ModalForm
      title={modItem?.id ? '修改报警限' : '新增报警限'}
      width={600}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnHidden: true,
        onCancel: handleModalShow,
        centered: true,
        maskClosable: false,
      }}
      open={modalOpen}
      submitter={{
        render: (props) => {
          let optBtns: any = [
            <Button
              className="base-button"
              key="cancel"
              onClick={handleModalShow}
            >
              取消
            </Button>,
            <Button
              className="base-button"
              type="primary"
              key="submit"
              onClick={() => props.form?.submit?.()}
            >
              确定
            </Button>,
          ];
          return optBtns;
        },
      }}
      onFinish={async (values) => {
        const sendData = {
          ...values,
          alarmSwitch: values.alarmSwitch ? '1' : '0',
        };
        let res = null;
        if (modItem?.id) {
          //编辑
          res = await monitorLimitsUpdate({
            ...sendData,
            id: modItem.id,
          });
        } else {
          //添加
          res = await monitorLimitsAdd(sendData);
        }
        if (res?.code === 200) {
          message.success('操作成功');
          refreshList();
          setModalOpen(false);
        }
        return true;
      }}
    >
      <Row gutter={20}>
        <Col span={12}>
          <ProFormText
            name="itemName"
            label="报警限名称"
            fieldProps={{
              maxLength: 50,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入报警限名称',
              },
            ]}
          />
        </Col>
        <Col span={12}>
          <ProFormDigit
            name="sort"
            label="排序"
            min={1}
            max={10000}
            fieldProps={{ precision: 0 }}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={12}>
          <ProFormSelect
            name="ageArea"
            label="年龄区间"
            placeholder="请选择年龄区间"
            allowClear
            rules={[
              {
                required: true,
                message: '请选择年龄区间',
              },
            ]}
            request={async () => {
              const res = await sysDictList({
                pageSize: 1000,
                pageNum: 1,
                itemClass: 'ageArea',
              });
              if (res?.code === 200) {
                return res.rows.map((its: any) => {
                  return {
                    label: its.itemName,
                    value: its.itemValue,
                  };
                });
              } else {
                return [];
              }
            }}
          />
        </Col>
        <Col span={12}>
          <ProFormSelect
            name="itemCode"
            label="报警限编码"
            placeholder="请选择报警限编码"
            allowClear
            rules={[
              {
                required: true,
                message: '请选择报警限编码',
              },
            ]}
            request={async () => {
              const res = await sysDictList({
                pageSize: 1000,
                pageNum: 1,
                itemClass: 'limits_item',
              });
              if (res?.code === 200) {
                return res.rows.map((its: any) => {
                  return {
                    label: its.itemName,
                    value: its.itemValue,
                  };
                });
              } else {
                return [];
              }
            }}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={12}>
          <ProFormSelect
            name="level"
            label="报警级别"
            placeholder="请选择报警级别"
            allowClear
            rules={[
              {
                required: true,
                message: '请选择报警级别',
              },
            ]}
            request={async () => {
              const res = await sysDictList({
                pageSize: 1000,
                pageNum: 1,
                itemClass: 'monitorLimitsLevel',
              });
              if (res?.code === 200) {
                return res.rows.map((its: any) => {
                  return {
                    label: its.itemName,
                    value: its.itemValue,
                  };
                });
              } else {
                return [];
              }
            }}
          />
        </Col>
        <Col span={12}>
          <ProFormSwitch
            name="alarmSwitch"
            label="报警开关"
            rules={[
              {
                required: true,
                message: '请选择报警开关',
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={12}>
          <ProFormDigit
            name="lowerLimit"
            label="最小值"
            min={1}
            max={100}
            fieldProps={{ precision: 0 }}
            rules={[
              {
                required: true,
                message: '请输入最小值',
              },
            ]}
          />
        </Col>
        <Col span={12}>
          <ProFormDigit
            name="upperLimit"
            label="最大值"
            min={1}
            max={100}
            fieldProps={{ precision: 0 }}
            rules={[
              {
                required: true,
                message: '请输入最大值',
              },
            ]}
          />
        </Col>
      </Row>
    </ModalForm>
  );
});

export default ItemOption;
